package exam.controller;

import com.alibaba.fastjson.JSONObject;
import exam.aopLogger.Log;
import exam.listener.MyEventService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/3/31 11:26
 * @since JDK 1.8
 */
@RestController
@Api("测试模块")
public class DemoController {

    @GetMapping("/test")
    @ApiOperation("测试模块")
    public JSONObject testJSONController(){
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("code",0);
        jsonObject.put("msg","success");
        return jsonObject;
    }

//    @PutMapping("putApp")
//    public JSONObject putController(@ApiParam("键值")@RequestParam("key")String key,
//                                    @ApiParam("值")@RequestParam("value")String value,
//                                    HttpServletResponse response, HttpServletRequest request){
//
//    }

    @Autowired
    private MyEventService myEventService;

    @GetMapping("testListener")
    @Log("事件测试")
    @ApiOperation("事件测试")
    public JSONObject testListenerController(@ApiParam("事件测试名称")@RequestParam("eventName")String enentName){
        myEventService.addEvent(enentName);
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("code",0);
        jsonObject.put("msg","事件发布成功");
        return jsonObject;
    }

    @GetMapping("servletTest")
    @Log("servlet监听测试")
    @ApiOperation("servlet监听测试")
    public JSONObject testServletController(HttpServletRequest request,HttpServletResponse response){
        JSONObject jsonObject=new JSONObject();
        Object name=request.getAttribute("name");
        jsonObject.put("name",name);
        return jsonObject;
    }

    @GetMapping("/total")
    @Log("获取在线人数")
    @ApiOperation("在线用户人数测试")
    public String getTotalUser(HttpServletRequest request) {
        Integer count = (Integer) request.getSession().getServletContext().getAttribute("count");
        return "当前在线人数：" + count;
    }


    @PostMapping("/test")
    public String getJSONController(){
        return "";
    }

}
